package com.wuxi.scheduler.service.impl;

import com.wuxi.scheduler.clients.SEERRobotsClient;
import com.wuxi.scheduler.pojo.seer.*;
import com.wuxi.scheduler.service.SeerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
@RequiredArgsConstructor
public class SeerServiceImpl implements SeerService {

    private final SEERRobotsClient seerRobotsClient;

    @Value("${seer.rds.postAction.config-id:WRF01-2}")
    private String CONFIG_ID;

    /**
     * 创建运单
     *
     */
    @Override
    public SeerRobotOrderResponse setOrder(OrderDto orderDto) {
        if (orderDto.getLocation() == null || orderDto.getLocation().isEmpty()) {
            return null;
        }
        Order order = new Order();
        order.setId(UUID.randomUUID().toString());
        order.setComplete(true); // todo:改成false
        if (orderDto.getVehicleId() != null) {
            order.setVehicle(orderDto.getVehicleId());
        }
        Block block = new Block();
        block.setBlockId(UUID.randomUUID().toString());
        block.setLocation(orderDto.getLocation());
        PostAction postAction = new PostAction();
        postAction.setConfigId(CONFIG_ID); // 修改为真实的configId
        block.setPostAction(postAction);
        List<Block> blocks = new ArrayList<>();
        blocks.add(block);
        order.setBlocks(blocks);
        if(orderDto.getPriority() != null &&  !orderDto.getLocation().isEmpty()){
            order.setPriority(orderDto.getPriority());
        }
        return seerRobotsClient.setOrder(order);
    }

    /**
     * 终止多个运单-通过idList
     */
    @Override
    public SeerRobotBaseResponse terminateOrderId(String id) {
        TerminalIdListOrder terminalOrder = new TerminalIdListOrder();
        List<String> idList = new ArrayList<>();
        idList.add(id);
        terminalOrder.setIdList(idList);
        terminalOrder.setDisableVehicle(false); // 默认此AGV能继续接单
        return seerRobotsClient.terminateOrderId(terminalOrder);
    }

    /**
     * 终止多个运单-通过vehicles
     */
    @Override
    public SeerRobotBaseResponse terminateOrderVechile(String vehicle) {
        TerminalVehiclesOrder terminalOrder = new TerminalVehiclesOrder();
        List<String> vehicles = new ArrayList<>();
        vehicles.add(vehicle);
        terminalOrder.setVehicles(vehicles);
        terminalOrder.setDisableVehicle(false); // 默认此AGV能继续接单
        return seerRobotsClient.terminateOrderVechile(terminalOrder);
    }

    /**
     * 获取机器人信息
     *
     */
    @Override
    public SeerRobotAlarmResponse getRobotsStatus() {
        String path = "report.rbk_report.alarms,report.vehicle_id";
        return seerRobotsClient.getRobotsStatus(path);
    }

    /**
     * 修改运单优先级(当前运单没有分派机器人时，可以下发修改运单的优先级)
     *
     */
    @Override
    public SeerRobotBaseResponse setPriority(String id, int priority) {
        Pirority pirority = new Pirority();
        pirority.setId(id);
        pirority.setPriority(priority);
        return seerRobotsClient.setPriority(pirority);
    }

    /**
     * 运单封口
     */
    @Override
    public SeerRobotBaseResponse markComplete(String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        OrderComplete orderComplete = new OrderComplete();
        orderComplete.setIdList(ids);
        return seerRobotsClient.markComplete(orderComplete);
    }

    /**
     * 通过运单id查询运单状态
     */
    @Override
    public SeerOrderStatusResponse orderDetails(String id) {
        String paths = "createTime,terminalTime,state,blocks.location,id,blocks.state";
        return seerRobotsClient.orderDetails(id, paths);
    }

    /**
     * 添加动作块
     */
    @Override
    public SeerRobotBaseResponse addBlocks(BlockDto blockDto) {
        Order order = new Order();
        if (blockDto.getLocation() == null || blockDto.getLocation().isEmpty()
                || blockDto.getId() == null || blockDto.getId().isEmpty()) {
            return null;
        }
        order.setId(blockDto.getId());
        order.setComplete(false);
        Block block = new Block();
        block.setBlockId(UUID.randomUUID().toString());
        block.setLocation(blockDto.getLocation());
        PostAction postAction = new PostAction();
        postAction.setConfigId(CONFIG_ID);
        block.setPostAction(postAction);
        List<Block> blocks = new ArrayList<>();
        blocks.add(block);
        order.setBlocks(blocks);
        return seerRobotsClient.addBlocks(order);
    }

    /**
     *  暂停机器人导航
     */
    @Override
    public SeerRobotBaseResponse gotoSitePause(List<String> vehicles) {
        return seerRobotsClient.gotoSitePause(vehicles);
    }

    /**
     *  继续机器人导航
     */
    @Override
    public SeerRobotBaseResponse gotoSiteResume(List<String> vehicles) {
        return seerRobotsClient.gotoSiteResume(vehicles);
    }


}
